package alkaid.oss.local.controller;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import alkaid.oss.local.domain.FileMetadata;
import alkaid.oss.local.domain.FileMetadataRepository;
import alkaid.oss.local.domain.FileMetadataService;
import cn.hutool.core.lang.Assert;

/**
 * 元数据API接口
 *  
 * @author Lucky Yang
 * @since 0.0.1
 */
@RestController
@RequestMapping("/file_metadatas")
public class FileMetadataController {
    @Autowired
    private FileMetadataRepository metadataRepository;
    @Autowired
    private FileMetadataService fileMetadataService;


    @GetMapping(value = "/{id}")
    public FileMetadataDto getById(@PathVariable Long id) {
        FileMetadata entity = fileMetadataService.findById(id);
        return FileMetadataMapper.INST.toDto(entity);
    }

    @GetMapping(value = "/ids")
    public List<FileMetadataDto> getByIds(@RequestParam Set<Long> ids) {
        return metadataRepository.findAllById(ids)
                .stream()
                .map(FileMetadataMapper.INST::toDto)
                .collect(Collectors.toList());
    }

    @PutMapping(value = "/{id}/name")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@PathVariable Long id, @RequestParam(value="name") String name) {
        Assert.notBlank(name);

        fileMetadataService.updateName(id, name);
    }    
}
